let numid; 
Page({
  //1、data------------------------------------------
  data:{
    nowSelect:'综合',
    nowSelectOver:'6d85a2b962b2e8200c12de415931a8d0',
    typeName:[],
    id:'',
    questid:'',
    signOk:false,
    lunbo:[],
    loginStatu:false

  },
    //2、生命周期函数------------------------------------
    onLoad(){
      this.typeList();
      this.sign()
      this.loginStatu()
      this.findLubo()
    },
    onShow(){
      this.loginStatu()
      this.addCollection()

    },
  //3、方法------------------------------------------
  daTi(){
    // //函数查找
    this.randoms()
   if(this.data.questid != ''){
        wx.redirectTo({
          url: '../questdetail/questdetail?id='+this.data.questid[numid]._id,
        })
   }
   else{
        wx.cloud.database().collection('questbank')
        .where({
         ktype:this.data.nowSelectOver
        })
        .get()
        .then(res => {
             console.log("success2",res)
             this.setData({
               questid:res.data
              })
         this.randoms()
         wx.redirectTo({
             url: '../questdetail/questdetail?id='+this.data.questid[numid]._id,
        })
    }).catch(err => {
        console.log("err",err)
    })
   }
  },
  //点击选择菜单触发
  selectType(e){
    this.setData({
      nowSelect:e.currentTarget.dataset.name,
      id : e.currentTarget.dataset.id,
    })
    this.TiBankList(this.data.id)

  },
  //题库列表
  TiBankList(ktypeid){
    wx.cloud.database().collection('questbank')
    .where({
      ktype:ktypeid
    })
    .get()
    .then(res => {
        console.log("success2",res)
        this.setData({
          questid:res.data
        })
        // console.log(this.data.questid.length)
       this.randoms()
       console.log(this.data.questid)

    }).catch(err => {
        console.log("err",err)
    })
  },
  //随机函数
  randoms(){
    numid = Math.random();           
    numid = numid*this.data.questid.length-1;  //0——10的随机数
    numid = Math.ceil(numid); 
    console.log(numid)
  },
  //类型列表
  typeList(){
    wx.cloud.database().collection('type')
    .get()
    .then(res => {
        console.log("success",res)
        this.setData({
          typeName:res.data,
        })
    }).catch(err => {
        console.log("err",err)
    })
  },
  //题集查找
  findQuestions(iid){
    wx.cloud.database().collection('question')
    .where({
      ktype: iid
    })
    .get()
    .then(res => {
        console.log("success",res)
        this.setData({
          questionList:res.data,
        })
    }).catch(err => {
        console.log("err",err)
    })
  },
  //登录状态判断
  loginStatu(){
    let user =  wx.getStorageSync('userInfo')
    if(user == null || user == ''){
        this.setData({
          loginStatu:false
        })
    }else{
        this.setData({
          loginStatu:true
        })
    }
  },
  pleaseLogin(){
    wx.showToast({
      icon:'none',
      title: '登录后可进行操作',
    })
},

  //签到
  sign(){
    let nowtime = new Date().getTime()
   const beforeTime = wx.getStorageSync('time')
   let onlytime = nowtime-beforeTime
   console.log(onlytime)
   if(onlytime>=54000000){
       wx.setStorageSync('time',nowtime)
       wx.setStorageSync('singOk',false)
      }

  },
 async  clickSign(){
    let singStatu =await wx.getStorageSync('singOk')
    console.log('fsdjkfjklsdfjk',singStatu === false)
    if(singStatu===true){
      wx.showToast({
            icon:'none',
            title: '今日已经签到',
        })
    }else if(singStatu===false){
      this.addScoreToUser()
      wx.setStorageSync('singOk',true)
       wx.showToast({
         icon:'none',
         title: '签到成功',
        })

    }


  },
  //分数更新到后台
  async addScoreToUser(){
    
    const db = wx.cloud.database().collection('user')
    
    // 获取原来分数   
    let user =  wx.getStorageSync('userInfo')
    console.log(user._openid)
    let {data} = await db.where({
        _openid:user._openid
    }).get()

    //签到+3分
    let score = data[0].score + 3

    //添加分数到数据库
    await db.where({
        _openid:user._openid
    }).update({
       data:{
         score
       }
    })

},
  //跳转错题本
  errTi(){
    wx.navigateTo({
      url: '/pages/wrong/wrong',
    })
  },
  //跳转排行榜
  matchList(){
    wx.navigateTo({
      url: '/pages/anlist/anlist',
    })
  },
  //跳转到收藏
  collection(){
    wx.navigateTo({
      url: '/pages/starme/starme',
    })
  },
    getSearch(){
      // console.log(e.detail.value)
      // let search = e.
      wx.navigateTo({
        url: '/pages/search/search'
      })
    }

  
  ,
  //把收藏的的添加
 async addCollection(){
    let coll = wx.getStorageSync('coll')
    if(coll==null){
      return
    }

    let db = wx.cloud.database().collection('collection')
    let user = wx.getStorageSync('userInfo') 
    
    if(user==null){return}
    //查询此用户的错题表
    let find = await db.where({
        _openid:user._openid
    }).get()
    console.log(find)

    //判断是否相同的题

    for(let i=0;i<coll.length;i++){
      //  console.log("01")
      for(let j=0;j<find.data.length;j++){
        // console.log("02")
        if(coll[i]==null){break}
        if(find.data[j].questionID==coll[i]._id){
          // console.log("03")
            return
        }
      }

      if(coll[i] != null)
      {
        let questionall= coll[i]
        let questionID = coll[i]._id

          //添加操作
             await db.add({
               data:{
                   questionID,
                   questionall
               }
           })
         }
    }
  },
  findLubo(){
    let db = wx.cloud.database().collection('rotation')
    db.orderBy('order','desc')
    .get().then(res => {
      this.setData({
        lunbo:res.data
      })
      // console.log('67',res)
    })
  }


})